#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2018/11/7 13:37
# @Author  : supuchun
# @Site    :
# @File    : insert_task.py

import datetime
import json
import logging
import redis

from get_task import update_run_times
from model.mns_demo import ali_mns
from model.mysql_model import MysqlElephantV2
from model.redis_setting import REDIS_HOST, REDIS_PORT, REDIS_PASSWORD
from model.simlog import Logger

r = redis.Redis(host=REDIS_HOST, port=REDIS_PORT, db=0, password=REDIS_PASSWORD, decode_responses=True)
obj_elev2 = MysqlElephantV2()

check_is_pure_pic_log = Logger('./log/check_is_pure_pic.log', logging.WARNING, logging.DEBUG)
surf_score_no_match_log = Logger('./log/surf_score_no_match.log', logging.WARNING, logging.DEBUG)
class insert_demo:
	def __init__(self):
		self.surf = 0.09568480300187
		self.send_queue = "elephant-test-one"
		self.insert_queue = "elephant-test-two"
	
	def get_data(self):
		q = ali_mns(self.send_queue, self.insert_queue)
		rs_info = q.get_message(self.insert_queue)
		# print rs_info
		rs = json.loads(rs_info)
		tabname = 'compare_' + str(rs['company_id'])
		compare_id = rs['compare_id']
		company_id = rs['company_id']
		task_id = rs['task_id']
		update_run_times(tabname, 2, compare_id)
		# print rs
		result_id = rs['result_id']
		re_from = rs['from']
		surf_score = rs['surf_score']
		n2_score = rs['n2_score']
		if re_from == "vcg_editor":
			asset_family = 1
		elif re_from == "vcg_creative":
			asset_family = 2
		elif re_from == "quanjing":
			asset_family = 7
		elif re_from == "veer":
			asset_family = 3
		else:
			asset_family = 100
		# print rs['compare_type']
		if rs['compare_type'] == 0:
			# print surf_score
		
			if surf_score > self.surf:
				
				# 检测是否是纯色图
				pic_value = self.check_is_pure_pic(result_id)
				if pic_value == None:
					msg_data = {}
					msg_data['table_name'] = 'compare_' + str(rs['company_id'])
					msg_data['compare_id'] = rs['compare_id']  #
					msg_data['task_id'] = rs['task_id']  #
					msg_data['clue_id'] = rs['clue_id']  #
					msg_data['client_date'] = rs['client_date']  #
					msg_data['score'] = n2_score  #
					msg_data['af_order'] = surf_score  #
					msg_data['pic_url'] = rs['img_url']  #
					msg_data['url'] = rs['url']  #
					msg_data['asset_family'] = asset_family  #
					msg_data['company_id'] = rs['company_id']  #
					msg_data['is_cover'] = rs['is_cover']  #
					msg_data['oss_url'] = rs['oss_url']  #
					msg_data['result_id'] = result_id  #
					msg_data['compare_type'] = rs['compare_type']
					# print msg_data
					try:
						self.insert_data(msg_data)
						update_run_times(tabname, 3, compare_id)
					except:
						print '出错了'
						return False
				else:
					print '纯色图'
					update_run_times(tabname, 15, compare_id)
					m_info = str(task_id) + " " + str(company_id)  + " " + str(compare_id)  + " " + str(result_id)
					check_is_pure_pic_log.info(m_info)
					return False
			else:
				print 'surf_score no match'
				update_run_times(tabname, 13, compare_id)
				m_info = str(task_id) + " " + str(company_id) + " " + str(compare_id) + " " + str(result_id)
				surf_score_no_match_log.info(m_info)
				return False
	def insert_data(self, rs):
		if rs['compare_type'] == 0:
			nowTime = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
			sql = """insert INTO elephant_v2.ele_result_one  SET
			score = %s, af_order = %s,spider_id = %s,task_id = "%s",company_id = "%s",clue_id = "%s",url = "%s",pic_url = "%s",oss_url = "%s",is_cover = "%s",result_id = "%s",client_date = "%s",add_date = "%s",asset_family = "%s" """ % \
			      (rs['score'], rs['af_order'], rs['compare_id'], rs['task_id'], rs['company_id'], rs['clue_id'],
			       rs['url'], rs['pic_url'], rs['oss_url'], rs['is_cover'], rs['result_id'], rs['clue_id'], nowTime,
			       rs['asset_family'])
			# print sql
			obj_elev2.cud(sql)
	
	def check_is_pure_pic(self, result_id):
		pic_key = 'pure_%s' % result_id
		pic_value = r.get(pic_key)
		return pic_value


while 1:
	a = insert_demo()
	a.get_data()

